<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="" prop="paramDoubleQuery">
        <el-input v-model="queryParams.paramDoubleQuery" placeholder="请输入组态名称/设备名称" prefix-icon="el-icon-search"
          clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
        style="float: right;">新增</el-button>
    </el-form>
    <el-row v-loading="loading">
      <el-col :span="cardSpan" v-for="item in bDeviceZtList" :key="item.id" style="padding: 2px;">
        <div style="width:auto;height:300px;">
          <el-card :body-style="{ padding: '5px' }">
            <span class="device-name">{{ item.pageName }}</span>
            <el-image :src="item.pageImage == null ? noImage : baseApi + item.pageImage" title="预览" class="ztImage"
              @click="handlePreview(item)" />
            <div style="padding: 5px;">
              <span>{{ item.pageName }}</span>
              <svg-icon icon-class="qrCode" style="width:25px;height:25px;cursor:pointer;float: right;"
                v-if="item.isShare == 1" @click="showCodeClick(item)" />
              <div class="bottom clearfix">
                <el-button type="text" @click="empower(item)">授权</el-button>
                <el-button type="text" @click="handleDelete(item)" v-if="isAdmin">删除</el-button>
                <el-button type="text" @click="handleClickZt(item)">编辑</el-button>
                <el-button type="text" @click="handlePreview(item)">预览</el-button>
              </div>
            </div>
          </el-card>
        </div>
      </el-col>
    </el-row>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" :page-sizes="[12, 24, 48]" :page-size="12" />
    <el-dialog title="授权" :visible.sync="selectUserDialog" width="50%" append-to-body :close-on-click-modal="false">
      <SelectDept ref="selectDept" :empowerDevice="empowerDevice" />
      <div slot="footer" class="dialog-footer">
        <el-button @click="selectUserDialog = false">关 闭</el-button>
      </div>
    </el-dialog>
    <el-dialog title="扫一扫" :visible.sync="dialogCode" width="20%">
      <vue-qr :logoSrc="imageUrl" :text="codeUrl" :size="350"></vue-qr>
    </el-dialog>
    <!-- 添加或修改云组态对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="页面图片">
          <image-upload v-model="form.pageImage" :limit="1" :fileSize="1" />
        </el-form-item>
        <el-form-item label="页面名称" prop="pageName">
          <el-input v-model="form.pageName" placeholder="请输入页面名称" />
        </el-form-item>
        <el-form-item label="页面大小" prop="pageSize">
          <el-select v-model="form.pageSize" placeholder="请选择页面大小" style="width:380px">
            <el-option v-for="item in fblOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分享状态" prop="isShare">
          <el-radio v-model="form.isShare" :label="1">分享</el-radio>
          <el-radio v-model="form.isShare" :label="0">不分享</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBDeviceZt, delBDeviceZt,addBDeviceZt} from "@/api/ghxx/bDeviceZt";
import noImage from '@/assets/image/no-zt.png'
import SelectDept from "./selectDept"
import vueQr from 'vue-qr'
import { getToken } from '@/utils/auth'
export default {
  name: "BDeviceZt",
  components: {
    SelectDept, vueQr
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 云组态表格数据
      bDeviceZtList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 12,
        orderByColumn: "id",
        isAsc: "desc",
        guid: null,
        pageName: null,
      },
      baseApi: process.env.VUE_APP_BASE_API,
      noImage: noImage,
      selectUserDialog: false,
      empowerDevice: {},
      dialogCode: false,
      imageUrl: require("@/assets/logo/logo.png"),
      codeUrl: '',
      isAdmin: false,
      cardSpan: 4,
      form: {},
      // 表单校验
      rules: {
        pageName: [
          { required: true, message: "页面名称不能为空", trigger: "blur" }
        ],
        pageSize: [
          { required: true, message: "页面大小不能为空", trigger: "blur" }
        ],
      },
      fblOptions: [{
        value: '960x600',
        label: '小屏幕:960x600'
      }, {
        value: '1024x768',
        label: '小屏幕:1024x768'
      }, {
        value: '1280x1024',
        label: '小屏幕:1280x1024'
      }, {
        value: '1366x768',
        label: 'PC网页:1366x768'
      }, {
        value: '1440x900',
        label: 'PC网页:1440x900'
      }, {
        value: '1920x960',
        label: 'PC网页:1920x960'
      }, {
        value: '1920x1080',
        label: 'PC网页:1920x1080'
      }, {
        value: '2048x858',
        label: '2K屏幕:2048x858'
      }, {
        value: '2048x1080',
        label: '2K屏幕:2048x1080'
      }, {
        value: '3840x2160',
        label: '4K屏幕:3840x2160'
      }, {
        value: '3656x2664',
        label: '2K屏幕:3656x2664'
      }, {
        value: '4096x3112',
        label: '4K屏幕:4096x3112'
      }, {
        value: 'custom',
        label: 'custom'
      }],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
    };
  },
  created() {
    this.getList();
    this.isAdmin = this.$auth.hasRole('admin');
    if (window.screen.width < 1680) {
      this.cardSpan = 6;
    } else {
      this.cardSpan = 4;
    }
  },
  methods: {
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "组态添加";
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        guid: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        deptIdStrs: null,
        deviceMac: null,
        deviceName: null,
        deviceZtData: null,
        isMainPage: null,
        pageName: null,
        pageSize: null,
        isShare: null,
        shareUrl: null,
        sharePass: null
      };
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addBDeviceZt(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    showCodeClick(row) {
      this.dialogCode = true;
      this.codeUrl = process.env.VUE_APP_BASE_URL+ '/fullscreen?guid=' + row.guid ;
    },
    //授权
    empower(row) {
      this.empowerDevice = row
      this.selectUserDialog = true;
    },
    //编辑
    handleClickZt(row) {
      window.open(process.env.VUE_APP_BASE_URL+ '/editor?guid=' + row.guid)
    },
    //预览
    handlePreview(row) {
      window.open(process.env.VUE_APP_BASE_URL + '/fullscreen?guid='+ row.guid)
    },
    /** 查询云组态列表 */
    getList() {
      this.loading = true;
      this.queryParams.deviceMac = this.$route.query.deviceImei
      listBDeviceZt(this.queryParams).then(response => {
        this.bDeviceZtList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id
      this.$modal.confirm('是否确认删除组态界面?').then(function () {
        return delBDeviceZt(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
  }
};
</script>
<style>
.time {
  font-size: 13px;
  color: #999;
  margin-right: 5px;
}

.bottom {
  margin-top: 13px;
  line-height: 12px;
  text-align: right;
}

.ztImage {
  width: 100%;
  height: 200px;
}

.ztImage:hover {
  cursor: pointer;
  transform: scale(1.1);
  transition: transform 0.8s ease-in-out
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}

.device-name {
  background-color: #5479f7;
  display: inline-block;
  color: #fff;
  text-align: center;
  height: 25px;
  line-height: 15px;
  font-size: 12px;
  padding: 4px 6px;
  position: absolute;
  margin: -5px;
  z-index: 2;
}
</style>